<?php

namespace Acme\StoreBundle\Controller\Admin;

use \Acme\StoreBundle\Controller\AdminController;
use Acme\StoreBundle\Entity\Post;

use Acme\StoreBundle\Form\Admin\PostType;

use Symfony\Component\HttpFoundation\Request;


class PostController extends AdminController {
    
    public function indexAction() {
        
        $repository = $this->getDoctrine()
            ->getRepository('StoreBundle:Post');
        $posts = $repository->findAll();
        
        
        return $this->render(
            'StoreBundle:Admin\Post:index.html.twig',
            array ('posts' => $posts)
        );
    }
    
    public function addAction(Request $request) {
        $user_id = 1;
        
        // Create form
        $post = new Post();
        $form = $this->createForm(new PostType(), $post);
        
        $form->handleRequest($request);
        
        if ($form->isValid()) {
           
            $form_data = $form->getData();
            
            $post->setTitle($form_data->getTitle());
            $post->setContent($form_data->getContent());
            $post->setIsDisplay(1);
            $post->setCreatedDate(new \DateTime());
            
            $em = $this->getDoctrine()->getManager();
            
            // Get user info
            $user = $em->getRepository('StoreBundle:User')->find($user_id);
            $post->setUser($user);
            
            // Save post
            $em->persist($post);
            
            $em->flush();
            
            return $this->redirect($this->generateUrl('admin_post_index'));
        }
        
        return $this->render(
            'StoreBundle:Admin\Post:add.html.twig',
            array ('form' => $form->createView())
        );
    }
    
    public function updateAction(Request $request, $id) {
        $user_id = 1;
        
        $em = $this->getDoctrine()->getManager();
        $post = $em->getRepository( 'StoreBundle:Post' )->find( $id );
        
        if ( ! $post ) {
            throw $this->createNotFoundException('Can not find the post');
        }
        
        $form = $this->createForm(new PostType(), $post);
        
        $form->handleRequest($request);
        if ( $form->isValid() ) {
            
            $form_data = $form->getData();
            
            $post->setTitle( $form_data->getTitle() );
            $post->setContent( $form_data->getContent() );
            
            $em = $this->getDoctrine()->getManager();
        
            // Get user info
            $user = $em->getRepository('StoreBundle:User')->find($user_id);
            $post->setUser($user);
            
            $em->persist($post);
            $em->flush();
            
            return $this->redirect($this->generateUrl('admin_post_index'));
        }
        
        return $this->render(
            'StoreBundle:Admin\Post:add.html.twig',
            array ( 'form' => $form->createView() )
        );
    }
    
    public function deleteAction( $id ) {
        $em = $this->getDoctrine()->getManager();
        $post = $this->getDoctrine()->getRepository('StoreBundle:Post')->find($id);
        
        if ( ! $post ) {
            throw $this->createNotFoundException('Not found this post');
        }
        
        $em->remove($post);
        $em->flush();
        return $this->redirect($this->generateUrl('admin_post_index'));
    }
}